package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.domain.CommentInfo;
import com.ruoyi.common.mapper.CommentInfoMapper;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteYLService;
import com.ruoyi.system.api.constant.YlStatus;
import com.ruoyi.system.service.ICommentInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户评论信息Service业务层处理
 *
 * @author jiabao
 * @date 2025-08-26
 */
@Service
public class CommentInfoServiceImpl extends ServiceImpl<CommentInfoMapper, CommentInfo> implements ICommentInfoService {


    @Resource
    private RemoteYLService remoteYLService;

    /**
     * 查询用户评论信息
     *
     * @param id 用户评论信息主键
     * @return 用户评论信息
     */
    @Override
    public CommentInfo selectCommentInfoById(Long id) {


        return this.getBaseMapper().selectOne(Wrappers.<CommentInfo>lambdaQuery()
                .eq(CommentInfo::getId, id)
        );
    }

    /**
     * 查询用户评论信息列表
     *
     * @param commentInfo 用户评论信息
     * @return 用户评论信息
     */
    @Override
    public List<CommentInfo> selectCommentInfoList(CommentInfo commentInfo) {
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        //管理员可以查询到全部（ID为1的）其他都是按照自己的UserId查询
        if (admin) {
           return this.getBaseMapper().selectList(Wrappers.<CommentInfo>lambdaQuery()
                    .eq(ObjectUtils.isNotEmpty(commentInfo.getSysUserId()), CommentInfo::getSysUserId, commentInfo.getSysUserId())
                    .eq(CommentInfo::getStatus, YlStatus.NORMAL.getCode()));
        }else {
          return   this.getBaseMapper().selectList(Wrappers.<CommentInfo>lambdaQuery()
                    .eq(CommentInfo::getSysUserId, SecurityUtils.getUserId())
                    .eq(ObjectUtils.isNotEmpty(commentInfo.getSysUserId()), CommentInfo::getSysUserId, commentInfo.getSysUserId())
                    .eq(CommentInfo::getStatus, YlStatus.NORMAL.getCode()));
        }

    }


    public CommentInfo selectByOrderNo(String orderNo) {
        return this.getBaseMapper().selectOne(Wrappers.<CommentInfo>lambdaQuery()
                .eq(CommentInfo::getOrderNo, orderNo)
                .eq(CommentInfo::getStatus, YlStatus.NORMAL.getCode())
        );
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult reviewTrue(String orderNo) {

        R<AjaxResult> r = remoteYLService.reviewTrue(orderNo, SecurityConstants.INNER);
        return getAjaxResult(orderNo, r);

    }

    @Override
    public AjaxResult reviewFalse(String orderNo, String reviewStatusDesc) {
        R<AjaxResult> r = remoteYLService.reviewFalse(orderNo, reviewStatusDesc, SecurityConstants.INNER);
        return getAjaxResult(orderNo, r);
    }

    private AjaxResult getAjaxResult(String orderNo, R<AjaxResult> r) {
        if (r.getCode() == Constants.SUCCESS) {
            AjaxResult r2 = r.getData();
            if (r2.isSuccess()) {
                //修改审核状态：
                CommentInfo commentInfo = this.selectByOrderNo(orderNo);
                commentInfo.setStatus(YlStatus.ABNORMAL.getCode());
                commentInfo.setUpdatedAt(LocalDateTime.now());
                commentInfo.setSysUserId(SecurityUtils.getUserId());
                this.saveOrUpdate(commentInfo);
            }
        }
        return r.getData();
    }
}
